package com.huluobo.a2205monthdemo.visitor

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.google.gson.Gson
import com.huluobo.a2205monthdemo.R
import com.huluobo.a2205monthdemo.adapter.VisitorAdapter
import com.huluobo.a2205monthdemo.databinding.ActivityVisitorBinding
import com.huluobo.base.bean.VisitorBean
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.RequestBody

@AndroidEntryPoint
class VisitorActivity : BaseActivity<ActivityVisitorBinding, VisitorViewModel>() {
    private lateinit var visitorAdapter: VisitorAdapter
    override fun initData() {
        val map = mutableMapOf<String, String>()
        map[Const.PARAM_USERNAME] = SPUtils.getInstance().getString(Const.PARAM_USERNAME)
        val json = Gson().toJson(map)
        val body = RequestBody.create(Const.MEDIA_TYPE.toMediaTypeOrNull(), json)
        vm.getAllVisitor(body)

        vm.getAllVisitorSuccess.observe(this) {
            val list = Gson().fromJson(it, VisitorBean::class.java)
            visitorAdapter.data.clear()
            visitorAdapter.data.addAll(list)
            visitorAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.visitorHeader.headerTitle.text = "来访预约"
        binding.visitorHeader.headerBack.setOnClickListener { finish() }
        binding.visitorHeader.headerBack.visibility = View.VISIBLE
        binding.visitorHeader.headerAdd.setOnClickListener {
            val intent = Intent(this, AddVisitorActivity::class.java)
            startActivity(intent)
        }
        binding.visitorHeader.headerAdd.visibility = View.VISIBLE

        binding.visitorRv.layoutManager = LinearLayoutManager(this)
        visitorAdapter = VisitorAdapter()
        binding.visitorRv.adapter = visitorAdapter
    }
}